
var leftPage = 1;
var leftIndex = 0;
var menuIndex = null;
var page = 1;
var mainIndex = null;
var runState = false;
var runParamName = ''
var MyMar;
// 文字走馬燈
function timedCount(index, str) {
    var elem = str;
    var thisContain = elem[index];
    var thisIntro = elem[index].getElementsByClassName(runParamName);
    thisIntro[0].style.left = '0px';
    function Marquee() {
        var left = thisIntro[0].style.left;
        if (thisContain.offsetWidth < thisIntro[0].offsetWidth) {
            if (parseInt(left) > -thisIntro[0].offsetWidth) {
                thisIntro[0].style.left = (parseInt(left) - 1) + 'px';
            } else {
                thisIntro[0].style.left = '0px'
            }
        }
    }
    MyMar = setInterval(Marquee, 40);
}
function MessOpt(page, totalPage, pos, index, run, paramName) {
    if (run == 'run' && paramName) {//文字走馬燈
        runState = true;
        runParamName = paramName;
    }
    $('#now_page').html(page);
    $('#total_page').html(totalPage);
    var rightIndex = typeof index == 'number' ? index : pos == 'next' ? 0 : modelIndex.pageSize - 1;
    right.initialization();
    if (!(pos == 'create' && !(typeof index == 'number'))) {
        right.toggleFocus(rightIndex);
    }
}
function modelInit() {
    //面包屑
    if (modelIndex.crumbs && modelIndex.crumbs.length) {
        TV.structure(modelIndex.crumbs, modelIndex.crumbStyle)
    }
    var indexList = new TV.CookieOpt().getOpt()
    if (indexList && JSON.stringify(indexList) != "{}") {
        console.log(indexList)
        page = indexList.page;
        mainIndex = indexList.index;
        menuIndex = indexList.menuIndex;
        getMenus(indexList.leftPage, indexList.leftIndex)
        if (typeof indexList.menuIndex == 'number' && !isNaN(indexList.menuIndex)) {
            console.log(indexList.menuIndex)
            menu.toggleFocus(indexList.menuIndex, indexList.index)
        }
    } else {
        getMenus();
        cmsIndex.handle('left');
    }
}

/*
 * 0.导航内容
 */
cmsIndex.modules[cmsIndex.moduleGuid(cmsIndex.addModule())] = {
    modName: 'left',
    modNum: cmsIndex.moduleGuid(),
    map: {
        right: 1,
        menu: 2
    }
};

var left = cmsIndex.modules[cmsIndex.moduleGuid()].module = (function () {
    var mod = {
        modName: 'menuList',
        modNum: cmsIndex.moduleGuid()
    },
        index = modelIndex.index,
        type = modelIndex.structure ? modelIndex.structure : 1,
        pageSize = modelIndex.menuPageSize,
        nowPage = 1,
        totalPage = 1,
        length = 0,
        containerId = modelIndex.menuContainerId,
        listName = modelIndex.menuListName;

    //左侧内容生成
    mod.paint = function (page, number) {
        if (page != undefined && number != undefined) {
            nowPage = page;
            index = number;
        }
        var me = mod,
            str = '';
        totalPage = parseInt(leftData.length % pageSize == 0 ? leftData.length / pageSize : leftData.length / pageSize + 1);
        // console.log(total_page, now_page)
        var size = (nowPage - 1) * pageSize;
        var data = leftData.slice(size, size + pageSize);
        // console.log(data, str)
        length = data.length;
        for (var i = 0; i < data.length; i++) {
            str += '<li data-id=' + data[i].id + ' class=' + listName + ' type_style=' + data[i].type_style + ' data_content=' + data[i].name + ' data_size=' + data[i].size + ' list_name=' + data[i].list_name;
            if (data[i].menuList && data[i].menuList.length) {
                str += " data-isMenu=1"
            }
            if (data[i].is_cur) {
                str += " data-isCur=1"
            }
            if (data[i].rowHeight) {
                str += " data_rowHeight=" + data[i].rowHeight
            }
            str += '>' + data[i].name + '</li>';
        }
        $('#' + containerId).html(str);
        me.toggleFocus();
        return me;
    };

    //动态操作
    mod.toggleFocus = function () {
        var me = mod;
        var elem = $('#' + containerId).children('.' + listName);
        $('#' + modelIndex.mainContainerId).html('')  //去掉会导致切换3个模块切换卡顿有问题
        if (modelIndex.id == 'interactive') {
            if (leftData[index].menuList && elem[index].getAttribute('data-id') == 1111) {
                $(".list-bg").addClass("bg-chage");
                $('#menu_bg').css("display", 'block')
                menu.paint(leftData[index].menuList, 0)
            } else {
                $(".list-bg").removeClass("bg-chage");
                $('#menu_bg').html('')
                $('#menu_bg').css("display", 'none')
            }
        }

        if (modelIndex.menuKey) {
            modelIndex.menuKey = ''
        }
        for (i = 0; i < length; i++) {
            // debugger;
            elem[i].className = listName;
        }
        if (index >= 0 && index <= length - 1) {
            elem[index].className = elem[index].className + ' focus_menu';
        }
        me.initialization();

        return me;
    };
    //赋值
    mod.initialization = function () {
        var me = mod;
        elem = $('#' + containerId).children('.' + listName);
        var id = elem[index].getAttribute('data-id');
        modelIndex.type = elem[index].getAttribute('type_style');
        // console.log(elem[index].getAttribute('data_rowHeight'))
        modelIndex.rowHeight = elem[index].getAttribute('data_rowHeight');
        modelIndex.mainlistName = elem[index].getAttribute('list_name');
        modelIndex.pageSize = parseInt(elem[index].getAttribute('data_size'));
        leftPage = nowPage;
        leftIndex = index;
        console.log(menuIndex)
        if (typeof menuIndex == 'number' && !isNaN(menuIndex)) {

            modelIndex.menuKey = $('#menu_bg').children('.menu-list')[menuIndex].getAttribute('data-id');

        }
        getMessage(id, page, 'create', mainIndex);
        mainIndex = null;
        page = 1;
        return me;
    };
    //操作
    mod.moveOp = function (directive) {

        var me = mod,
            flag = 0;
        menuIndex = null;
        if ((directive == 'down' && type == 2) || (directive == 'forwards' && type == 1)) {
            // console.log(modelIndex.mainlistName)
            if (modelIndex.mainlistName) {
                // console.log(modelIndex.id, elem, index, elem[index].getAttribute('data-id'))
                if (modelIndex.id == 'interactive' && elem[index].getAttribute('data-id') == 1111) {
                    cmsIndex.handle('menu');
                    menu.toggleFocus(0);
                    return
                }
                // console.log($('#' + modelIndex.mainContainerId).children("." + modelIndex.mainlistName).length)
                if ($('#' + modelIndex.mainContainerId).children("." + modelIndex.mainlistName).length) {
                    result = cmsIndex.handle('right');
                    result.toggleFocus(0);
                } else if (directive == 'forwards' && modelIndex.type == '0') {
                    result = cmsIndex.handle('right');
                    result.toggleFocus(0);
                }
            }
            return;
        } else if ((directive == "up" && type == 1) || (directive == "backwards" && type == 2)) {//减减减
            // console.log('-----')
            if (--index < 0 && nowPage != 1) {
                index = pageSize - 1;
                nowPage--;
                flag = 1;
            } else if (index < 0 && nowPage == 1 && totalPage > 1) {
                index = 0;
            } else if (totalPage == 1 && index < 0) {
                index = length - 1;
            }
        } else if (directive == "down" || directive == 'forwards') {//加加加
            // console.log('+++++')
            if (++index == pageSize && totalPage != nowPage) {
                nowPage++;
                index = 0;
                flag = 1;
            } else if (nowPage == totalPage && index == length && totalPage > 1) {
                index = length - 1;
            } else if (totalPage == 1 && index == length) {
                index = 0;
            }

        }

        if (flag) {
            me.paint();
        } else {
            me.toggleFocus();
        }

        return me;
    };

    //电视操作
    mod.action = function (directive) {
        var me = mod;
        switch (directive) {
            case 'enter':
                break;
            case 'back':
                if (modelIndex.disModel) {
                    Utility.ioctlWrite("skyworthKey", "Key2VOD");
                } else {
                    history.go(-1);
                }
                break;
        }
        return me;
    };

    mod.grabEvent = function (key) {
        var me = mod;
        var keyJson = TV.GetKey(key);
        if (keyJson.keyState && keyJson.keyState != 'enter' && keyJson.keyState != 'back') {
            me.moveOp(keyJson.keyState);
        } else {
            me.action(keyJson.keyState)
        }
        return keyJson.isBlock;
    };
    return mod;
})();


/*
 * 1.右侧区
 */

cmsIndex.modules[cmsIndex.moduleGuid(cmsIndex.addModule())] = {
    modName: 'right',
    modNum: cmsIndex.moduleGuid(),
    map: {
        left: 0,
        menu: 2
    }
};

var right = cmsIndex.modules[cmsIndex.moduleGuid()].module = (function () {
    var mod = {
        modName: "pageList",
        modNum: cmsIndex.moduleGuid()
    },
        index = 0, //位置
        cleanFlag = 0,
        isLike = modelIndex.isLike,//是否开启点赞功能
        structure = modelIndex.structure,
        status = '1',//是否在外框
        length = 0, //真实长度
        nowPage = 1, //当前页数
        totalPage = 1,  //总页数
        type = '',//类型
        listName = '',  //模块名
        pageSize = 1,//模块数量
        rowHeight = modelIndex.rowHeight,//多行模块行数
        containerId = modelIndex.mainContainerId; //包含模块的标签id

    mod.cleanCSS = function () {
        var elem = $("#" + containerId).children("." + listName);
        var elemCur = $("#" + containerId).children(".cur")
        for (i = 0; i < length; i++) {
            elem[i].className = listName;
            if (elemCur.length) {
                elemCur[i].className = 'cur';
            }

        }
    }
    //操作
    mod.toggleFocus = function (number) {
        var me = mod;
        if (number != undefined) {
            index = number;
        }
        if (type == '0') {
            var elem = $("#" + containerId);
            elem[index].className = elem[index].className + ' active';
        } else {
            var elem = $("#" + containerId).children("." + listName);

            var elemCur = $("#" + containerId + " .cur")
            for (i = 0; i < length; i++) {
                elem[i].className = listName;
                if (elemCur.length) {
                    elemCur[i].className = 'cur';
                }
            }
            if (status == '0') {
                elemCur[index].className = elemCur[index].className + ' curActive'
            } else if (index >= 0 && index < length) {
                elem[index].className = elem[index].className + ' active';
                // 走馬燈
                if (runState == true) {
                    clearInterval(MyMar);
                    timedCount(index, elem);
                }
            }
            if (cleanFlag) {
                cleanFlag = 0;
                me.cleanCSS();
            }
        }



        return me;
    };
    //赋值
    mod.initialization = function () {
        var me = mod;
        listName = modelIndex.mainlistName;
        pageSize = modelIndex.pageSize;
        containerId = modelIndex.mainContainerId;
        nowPage = parseInt(document.getElementById('now_page').innerHTML);
        totalPage = parseInt(document.getElementById('total_page').innerHTML);
        length = $('#' + containerId).children("." + listName).length;
        isLike = $('.focus_menu').attr('data-iscur') ? true : false;
        type = modelIndex.type;
        rowHeight = modelIndex.rowHeight;//多行模块行数
        return me;
    };
    //index 位置调整
    mod.moveOp = function (directive) {
        var me = mod;

        if (modelIndex.type == '0') {
            // console.log(directive)
            if (directive == 'down') {
                TV.articleMove(directive, modelIndex.thisContain, modelIndex.thisDetail);
            } else if (directive == 'up') {
                TV.articleMove(directive, modelIndex.thisContain, modelIndex.thisDetail);
            } else if (directive == 'backwards') {
                clearInterval(articleRun);
                var elem = $("#" + containerId);
                elem[index].className = containerId;
                result = cmsIndex.handle('left');
                result.toggleFocus(1);
            }
        }
        //  else if (modelIndex.id == 'sport' && $('.focus_menu')[0].getAttribute('data-id') == '279' && (index == 2 || index == 5) && directive == 'forwards') {
        //     me.cleanCSS();
        //     $('.sport-img').addClass("active");
        //     index = -279;
        // } 
        // else if (modelIndex.id == 'sport' && $('.focus_menu')[0].getAttribute('data-id') == '279' && index == -279) {
        //     $('.sport-img').removeClass("active");
        //     if (directive == 'backwards') {
        //         index = 2;
        //         me.toggleFocus(2)
        //     } else if (directive == 'up') {
        //         cmsIndex.handle('left');
        //     }
        // }
        else {
            // console.log(modelIndex.rowHeight)
            var isMenu = $('.focus_menu').attr('data-isMenu');
            var json = new TV.ListOperation({
                isMenu: isMenu,
                type: type,
                direction: 'left',
                structure: structure,
                key: directive,
                isLike: isLike,
                status: status,
                nowPage: nowPage,
                totalPage: totalPage,
                pageSize: pageSize,
                length: length,
                index: index,
                rowHeight: rowHeight
            })

            var message = json.operation();
            // console.log(message)
            index = message.index;
            status = message.status;
            if (message.position != '') {
                var id = $('.focus_menu').attr('data-id');
                getMessage(id, message.nowPage, message.position);  //下一页
            }
            // console.log(message.direction)
            if (message.direction) {
                cmsIndex.handle(message.direction);
                if (message.direction == 'menu') {
                    menu.toggleFocus();
                    // menu.messageOpt();
                }
                cleanFlag = 1;
            }
            mod.toggleFocus();
            return me;
        }

    };
    mod.action = function (directive) {
        var me = mod;
        switch (directive) {
            case 'back':
                if (modelIndex.disModel) {
                    Utility.ioctlWrite("skyworthKey", "Key2VOD");
                } else {
                    history.go(-1);
                }
                break;
            case 'enter':
                // console.log(nowPage, index, leftPage, leftIndex)
                if (isCache) {
                    console.log(menuIndex)
                    var cookieTv = new TV.CookieOpt({
                        index: index,
                        page: nowPage,
                        leftPage: leftPage,
                        leftIndex: leftIndex,
                        menuIndex: typeof menuIndex == 'number' ? menuIndex : null
                    })
                    var indexList = cookieTv.setOpt();
                }

                var id = $('.focus_menu').attr('data-id');
                direEnter(status, id, index);

                break;
        }
        return me;
    };
    mod.grabEvent = function (key) {
        var me = mod;
        var keyJson = TV.GetKey(key);
        if (keyJson.keyState && keyJson.keyState != 'enter' && keyJson.keyState != 'back') {
            me.moveOp(keyJson.keyState);
        } else {
            me.action(keyJson.keyState)
        }
        return keyJson.isBlock;


    };

    return mod;
})();

/*
 * 2.导航区
 */

cmsIndex.modules[cmsIndex.moduleGuid(cmsIndex.addModule())] = {
    modName: 'menu',
    modNum: cmsIndex.moduleGuid(),
    map: {
        left: 0,
        right: 1
    }
};

var menu = cmsIndex.modules[cmsIndex.moduleGuid()].module = (function () {
    var mod = {
        modName: 'menuList',
        modNum: cmsIndex.moduleGuid()
    },
        index = 0,
        length = 0,
        containerId = "menu_bg",
        listName = "menu-list",
        cleanFlag = false,
        messFlag = false;

    //导航内容生成
    mod.paint = function (data, number) {
        var me = mod;
        if (number != undefined) {
            index = number;
        }
        // console.log(data)
        str = '';
        length = data.length;
        for (var i = 0; i < data.length; i++) {
            str += '<li data-id=' + data[i].id  + ' class=' + listName + '>' + data[i].title + '</li>';
        }
        $('#' + containerId).html(str);
        return me
    }

    //动态操作
    mod.toggleFocus = function (number, mainIndex) {
        var me = mod;
        if (number != undefined) {
            index = number;
        }
        var elem = $('#' + containerId).children('.' + listName);
        for (i = 0; i < length; i++) {
            // debugger;
            elem[i].className = listName;
        }
        if (index >= 0 && index <= length - 1 && !cleanFlag) {
            elem[index].className = elem[index].className + ' menu_active';
        }
        if (!cleanFlag && !messFlag) {
            me.messageOpt(mainIndex)
        }
        if (cleanFlag) {
            cleanFlag = !cleanFlag
        }
        if (messFlag) {
            messFlag = !messFlag
        }


        return me;
    };

    //操作
    mod.moveOp = function (directive) {
        var me = mod;
        // console.log(modelIndex.structure, index, directive)
        if (modelIndex.structure == 1 && directive == "backwards" || modelIndex.structure == 2 && directive == "up") { //左
            if (index > 0) {
                index--;
            } else {
                cleanFlag = true;
                cmsIndex.handle('left');
                left.toggleFocus();
            }
        } else if (modelIndex.structure == 1 && directive == 'forwards' || modelIndex.structure == 2 && directive == 'down') { //右
            if (index < length - 1) {
                index++
            }
        } else if (modelIndex.structure == 1 && directive == 'down' || modelIndex.structure == 2 && directive == 'forwards') {
            if ($('#' + modelIndex.mainContainerId).children("." + modelIndex.mainlistName).length) {
                messFlag = true;
                cmsIndex.handle('right');
                right.toggleFocus(0);
            }
            // return
        }
        menuIndex = index;
        me.toggleFocus();
        return me;
    };

    mod.action = function (directive) {
        var me = mod;
        switch (directive) {
            case 'back':
                history.go(-1);
                break;
            case 'enter':
                break;
        }
        return me;
    };

    mod.messageOpt = function (mainIndex) {
        var mIndex;
        // console.log(mainIndex)
        if (typeof mainIndex == 'number') {
            mIndex = mainIndex;
        }
        // console.log(mIndex)
        var me = mod;
        var id = $('.focus_menu').attr('data-id');
        modelIndex.menuKey = $('.menu_active').attr('data-id');
        getMessage(id, 1, 'create', mIndex);
        return me;
    }

    mod.grabEvent = function (key) {
        var me = mod;
        var keyJson = TV.GetKey(key);
        if (keyJson.keyState && keyJson.keyState != 'enter' && keyJson.keyState != 'back') {
            me.moveOp(keyJson.keyState);
        } else {
            me.action(keyJson.keyState)
        }
        return keyJson.isBlock;
    };
    return mod;
})();
$(window).unload(function () {
    if (modelIndex.isVideo && mp) {
        mp.stop();
    }
});
cmsIndex.run();